package views;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;

import controllers.updateControllers.UpdateVliegtuigenController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.UpdateVliegtuigenModel;
import core.models.domainModel.Vliegtuig;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateVliegtuigenView extends JFrame {
	private static final long serialVersionUID = 1L;
	private UpdateVliegtuigenModel _model;
	private JTextField txt_model;
	private JTextField txt_type;
	private JTextField txt_aantalPiloten;
	private JTextField txt_aantalStewards;
	private JTextField txt_aantalZetels;
	private JTextField txt_aantalRijenBusiness;
	private JTextField txt_aantalRijenEconomy;
	public UpdateVliegtuigenView(UpdateVliegtuigenModel model) {
		
		_model = model;
		
		JLabel lblVliegtuigWijzigen = new JLabel("Vliegtuig wijzigen:");
		
		JLabel lblModel = new JLabel("Model:");
		
		JLabel lblType = new JLabel("Type:");
		
		txt_model = new JTextField();
		txt_model.setColumns(10);
		
		txt_type = new JTextField();
		txt_type.setColumns(10);
		
		JLabel lblAantalPilotenNodig = new JLabel("Aantal piloten nodig:");
		
		JLabel lblAantalStewardsNodig = new JLabel("Aantal stewards nodig:");
		
		JLabel lblAantalZetelsPer = new JLabel("Aantal zetels per rij:");
		
		JLabel lblAantalRijenBusiness = new JLabel("Aantal rijen business:");
		
		JLabel lblAantalRijenEconomy = new JLabel("Aantal rijen economy:");
		
		txt_aantalPiloten = new JTextField();
		txt_aantalPiloten.setColumns(10);
		
		txt_aantalStewards = new JTextField();
		txt_aantalStewards.setColumns(10);
		
		txt_aantalZetels = new JTextField();
		txt_aantalZetels.setColumns(10);
		
		txt_aantalRijenBusiness = new JTextField();
		txt_aantalRijenBusiness.setColumns(10);
		
		txt_aantalRijenEconomy = new JTextField();
		txt_aantalRijenEconomy.setColumns(10);
		
		txt_model.setText(model.getGeselecteerdVliegtuig().get_model());
		txt_type.setText(model.getGeselecteerdVliegtuig().get_type());
		txt_aantalPiloten.setText(Integer.toString(model.getGeselecteerdVliegtuig().get_aantalPilotenNodig()));
		txt_aantalRijenBusiness.setText(Integer.toString(model.getGeselecteerdVliegtuig().get_aantalRijenBusiness()));
		txt_aantalRijenEconomy.setText(Integer.toString(model.getGeselecteerdVliegtuig().get_aantalRijenEconomy()));
		txt_aantalStewards.setText(Integer.toString(model.getGeselecteerdVliegtuig().get_aantalStewardsNodig()));
		txt_aantalZetels.setText(Integer.toString(model.getGeselecteerdVliegtuig().get_aantalZetelsPerRij()));
		
		JButton btnWijzigingDoorvoeren = new JButton("Wijziging doorvoeren");
		btnWijzigingDoorvoeren.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (Validatie(UpdateVliegtuigenView.this) == true)
				{
					Vliegtuig vl = new Vliegtuig();
					vl.set_vliegtuigID(_model.getGeselecteerdVliegtuig().get_vliegtuigID());
					vl.set_aantalPilotenNodig(Integer.parseInt(txt_aantalPiloten.getText()));
					vl.set_aantalRijenBusiness(Integer.parseInt(txt_aantalRijenBusiness.getText()));
					vl.set_aantalRijenEconomy(Integer.parseInt(txt_aantalRijenEconomy.getText()));
					vl.set_aantalStewardsNodig(Integer.parseInt(txt_aantalStewards.getText()));
					vl.set_aantalZetelsPerRij(Integer.parseInt(txt_aantalZetels.getText()));
					vl.set_model(txt_model.getText());
					vl.set_type(txt_type.getText());
					UpdateVliegtuigenController.handleView(vl);
					JOptionPane.showMessageDialog(null, "wijziging succesvol doorgevoerd");
				}
				
				
			}
		});
		
		JButton btnAnnuleren = new JButton("Annuleren");
		btnAnnuleren.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UpdateVliegtuigenController.returnToVliegtuigenView();
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblVliegtuigWijzigen)
						.addComponent(lblAantalRijenBusiness)
						.addComponent(lblAantalRijenEconomy)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblModel)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txt_model, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblType)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txt_type, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblAantalStewardsNodig)
								.addComponent(lblAantalZetelsPer))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(txt_aantalZetels, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txt_aantalStewards, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txt_aantalRijenBusiness, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txt_aantalRijenEconomy, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblAantalPilotenNodig)
							.addGap(30)
							.addComponent(txt_aantalPiloten, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnWijzigingDoorvoeren)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnAnnuleren, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(152, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblVliegtuigWijzigen)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblModel)
						.addComponent(txt_model, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblType)
						.addComponent(txt_type, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAantalPilotenNodig)
						.addComponent(txt_aantalPiloten, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAantalStewardsNodig)
						.addComponent(txt_aantalStewards, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAantalZetelsPer)
						.addComponent(txt_aantalZetels, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAantalRijenBusiness)
						.addComponent(txt_aantalRijenBusiness, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAantalRijenEconomy)
						.addComponent(txt_aantalRijenEconomy, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnWijzigingDoorvoeren)
						.addComponent(btnAnnuleren))
					.addContainerGap(19, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}
	
	public boolean Validatie(UpdateVliegtuigenView scherm)
	{
		if (txt_model.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_model, "model");
			return false;
		}
		if (txt_type.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_type, "type");
			return false;
		}
		
		if (txt_aantalPiloten.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_aantalPiloten, "aantal piloten");
			return false;
		}
		
		if (txt_aantalRijenBusiness.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_aantalRijenBusiness, "aantal rijen business class");
			return false;
		}
		
		if (txt_aantalRijenEconomy.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_aantalRijenEconomy,"aantal rijen economy class");	
			return false;
		}
		
		if (txt_aantalStewards.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_aantalStewards,"aantal stewards");
			return false;
		}
		
		if (txt_aantalZetels.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_aantalZetels,"aantal zetels");
			return false;
		}
		
		if (txt_aantalZetels.getText() != "")
		{
			try
			{
				Integer.parseInt(scherm.txt_aantalZetels.getText());
			}
			catch (NumberFormatException nfe)
			{
				ValidatieService.isInteger(scherm.txt_aantalZetels, "aantal zetels");
				return false;
			}
		}
		
		if (txt_aantalStewards.getText() != "")
		{
			try
			{
				Integer.parseInt(scherm.txt_aantalStewards.getText());
			}
			catch (NumberFormatException nfe)
			{
				ValidatieService.isInteger(scherm.txt_aantalStewards, "aantal stewards");
				return false;
			}
		}
		
		if (txt_aantalRijenEconomy.getText() != "")
		{
			try
			{
				Integer.parseInt(scherm.txt_aantalRijenEconomy.getText());
			}
			catch (NumberFormatException nfe)
			{
				ValidatieService.isInteger(scherm.txt_aantalRijenEconomy, "aantal rijen economy class");
				return false;
			}
		}
		
		if (txt_aantalRijenBusiness.getText() != "")
		{
			try
			{
				Integer.parseInt(scherm.txt_aantalRijenBusiness.getText());
			}
			catch (NumberFormatException nfe)
			{
				ValidatieService.isInteger(scherm.txt_aantalRijenBusiness, "aantal rijen business class");
				return false;
			}
		}
		
		if (txt_aantalPiloten.getText() != "")
		{
			try
			{
				Integer.parseInt(scherm.txt_aantalPiloten.getText());
			}
			catch (NumberFormatException nfe)
			{
				ValidatieService.isInteger(scherm.txt_aantalPiloten, "aantal piloten");
				return false;
			}
		}
		return true;
	}
}
